/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.knezevic.realestate.pages;

import com.knezevic.realestate.Advert;
import com.knezevic.realestate.Identifiable;
import com.knezevic.realestate.Member;
import com.knezevic.realestate.Privilege;
import com.knezevic.realestate.TypeOfAdvert;
import com.knezevic.realestate.beans.AdvertBean;
import com.knezevic.realestate.beans.MemberBean;
import com.knezevic.realestate.service.Adverts;
import com.knezevic.realestate.service.Members;
import com.knezevic.realestate.util.Formats;
import java.text.Format;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 *
 * @author Nemanja
 */
public class AdminHome {

    @Inject
    private Members members;
    private Member currentMember;
    @Inject
    private Adverts adverts;
    @Persist("flash")
    private Collection<Advert> allAdverts;
    @Persist("flash")
    private Collection<Member> allMembers;
    @Property
    @Persist("flash")
    private String message;
    @Property
    @Persist("flash")
    private String message2;
    private MemberBean mem;
    private AdvertBean advert;
    @InjectComponent
    private Zone zone;
    @InjectComponent
    private Zone zone2;
    @Property
    private String search;
    @InjectPage
    private AdminHome adminHome;
    @Persist
    private long id;
    @Persist
    private int zoneStatus;

    public ValueEncoder getMemberEncoder(){
        return new ValueEncoder<Member>() {

            public String toClient(Member value) {
               return value.getUsername();
            }

            public Member toValue(String clientValue) {
                for (Member member : members.retrieve()) {
                    if(member.getUsername().equals(clientValue)){
                        return member;
                    }
                }
                throw new IllegalArgumentException("Received key \"" + clientValue
						+ "\" which has no counterpart in this collection: " );
            }
        };
    }
    public ValueEncoder getAdvertEncoder(){
        return new ValueEncoder<Advert>() {

            public String toClient(Advert value) {
                Long key = Identifiable.class.cast(value).getID();
                return key.toString();
            }

            public Advert toValue(String clientValue) {
                Long key = new Long(clientValue);
                for (Advert advert : adverts.retrieve()) {
                    if(Identifiable.class.cast(clientValue).getID()==(key)){
                        return advert;
                    }
                }
                 throw new IllegalArgumentException("Received key \"" + clientValue
						+ "\" which has no counterpart in this collection: " );
            }
        };
    }
     public void onActivate(long id){
         setCurrentMember(members.retrieve(id));
        }
     
     public long onPassivate(){
         return id;
     }

    Object onActionFromShowAllAdvertsSelling() {
        List<Advert> newList = new LinkedList<Advert>();
        for (Advert a : adverts.retrieve()) {
            if (a.getTypeOfAdvert().equals(TypeOfAdvert.Selling)) {
                newList.add(a);
            }
        }
        setAllAdverts(newList);
        if (newList.isEmpty()) {
            message = "No result!";
        }
        zoneStatus = 1;
        return zone.getBody();
    }

    Object onActionFromShowAllAdvertsRenting() {
        List<Advert> newList = new LinkedList<Advert>();
        for (Advert a : adverts.retrieve()) {
            if (a.getTypeOfAdvert().equals(TypeOfAdvert.Renting)) {
                newList.add(a);
            }
        }
        setAllAdverts(newList);
        if (newList.isEmpty()) {
            message = "No result!";
        }
        zoneStatus = 2;
        return zone.getBody();
    }

    Object onActionFromShowAllAdvertsBuying() {
        List<Advert> newList = new LinkedList<Advert>();
        for (Advert a : adverts.retrieve()) {
            if (a.getTypeOfAdvert().equals(TypeOfAdvert.Buying)) {
                newList.add(a);
            }
        }
        setAllAdverts(newList);
        if (newList.isEmpty()) {
            message = "No result!";
        }
        zoneStatus = 3;
        return zone.getBody();
    }
    Object onActionFromShowAllAdvertsPublished() {
        List<Advert> newList = new LinkedList<Advert>();
        for (Advert a : adverts.retrieve()) {
            if (a.isConfirmed()) {
                newList.add(a);
            }
        }
        setAllAdverts(newList);
        if (newList.isEmpty()) {
            message = "No result!";
        }
        zoneStatus = 4;
        return zone.getBody();
    }
    Object onActionFromShowAllAdvertsUnpublished() {
        List<Advert> newList = new LinkedList<Advert>();
        for (Advert a : adverts.retrieve()) {
            if (!a.isConfirmed()) {
                newList.add(a);
            }
        }
        setAllAdverts(newList);
        if (newList.isEmpty()) {
            message = "No result!";
        }
        zoneStatus = 5;
        return zone.getBody();
    }

    Object onActionFromShowAllAdverts() {
        List<Advert> newList = new LinkedList<Advert>();
        for (Advert a : adverts.retrieve()) {
            newList.add(a);

        }
        setAllAdverts(newList);
        zoneStatus = 6;
        return zone.getBody();
    }

    Object onActionFromPublish(long id) {
        Advert advert = adverts.retrieve(id);
        advert.setConfirmed(true);
        adverts.update(advert);
        return refresh(zoneStatus);
    }

    Object onActionFromUnpublish(long id) {
        Advert advert = adverts.retrieve(id);
        advert.setConfirmed(false);
        adverts.update(advert);
        return refresh(zoneStatus);
    }

    Object onActionFromChangePrivilege(long id) {
        Member memb = members.retrieve(id);
        if (memb.getPrivilege().equals(Privilege.REGULAR)) {
            memb.setPrivilege(Privilege.ADMINISTRATOR);
            members.update(memb);
            message2 = "You have successfully changed privilege for member with USERNAME: "+memb.getUsername()+"!";
            return zone2;
        }
        message2 = "You can not change privilege for admin!";
        return zone2;

    }

    Object onActionFromLogout() {
        return Login.class;
    }

    /**
     * @return the allAdverts
     */
    public Collection<Advert> getAllAdverts() {
        return allAdverts;
    }

    /**
     * @param allAdverts the allAdverts to set
     */
    public void setAllAdverts(Collection<Advert> allAdverts) {
        this.allAdverts = allAdverts;
    }

    /**
     * @return the advert
     */
    public AdvertBean getAdvert() {
        return advert;
    }

    /**
     * @param advert the advert to set
     */
    public void setAdvert(AdvertBean advert) {
        this.advert = advert;
    }

    public Format getDateFormat() {
        return Formats.getDateFormat();
    }

    /**
     * @return the allMembers
     */
    public Collection<Member> getAllMembers() {
        return allMembers;
    }

    /**
     * @param allMembers the allMembers to set
     */
    public void setAllMembers(Collection<Member> allMembers) {
        this.allMembers = allMembers;
    }

    /**
     * @return the mem
     */
    public MemberBean getMem() {
        return mem;
    }

    /**
     * @param mem the mem to set
     */
    public void setMem(MemberBean mem) {
        this.mem = mem;
    }


   Object onSubmit() {

        List<Member> newList = new LinkedList<Member>();
        if (search == null) {
            adminHome.setAllMembers(members.retrieve());
            return adminHome;
        } else {
            CharSequence cs = search.toLowerCase();
            for (Member member : members.retrieve()) {
                if (member.getSurname().toLowerCase().contains(cs) || member.getName().toLowerCase().contains(cs) || member.getUsername().toLowerCase().contains(cs)) {
                    newList.add(member);
                }
            }
            if (newList.isEmpty()) {
                message2 = "No search result!";
                adminHome.setAllMembers(newList);
                return adminHome;
            } else {
                adminHome.setAllMembers(newList);
                return adminHome;
            }

        }

    }

    /**
     * @param id the id to set
     */
    public void setID(long id) {
        this.id = id;
    }

    /**
     * @return the id
     */
    public long getID() {
        return Identifiable.class.cast(getCurrentMember()).getID();
    }

    private Object refresh(int status){
        switch(status){
            case 1: return onActionFromShowAllAdvertsSelling();
            case 2: return onActionFromShowAllAdvertsRenting();
            case 3: return onActionFromShowAllAdvertsBuying();
            case 4: return onActionFromShowAllAdvertsPublished();
            case 5: return onActionFromShowAllAdvertsUnpublished();
            case 6: return onActionFromShowAllAdverts();
            default: return null;
        }
    }

    /**
     * @return the currentMember
     */
    public Member getCurrentMember() {
        return currentMember;
    }

    /**
     * @param currentMember the currentMember to set
     */
    public void setCurrentMember(Member currentMember) {
        this.currentMember = currentMember;
    }

}
